<template>
<div>
<template>
<!-- 添加学院隐藏端口 -->
<el-dialog title="添加学院" :visible.sync="dialogFormVisible">
  <el-form :model="department">
    <el-form-item label="学院名" label-width="120px">
      <el-input v-model="department.departmentName" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAdddepartment()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAdddepartment()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改学院隐藏端口 -->
  <el-dialog title="修改学院" :visible.sync="updateDialogFormVisible">
  <el-form :model="udepartment">
    <el-form-item label="学院名" label-width="120px">
      <el-input v-model="udepartment.departmentName" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdatedepartment()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedepartment()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加学院按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="adddepartment()">增加学院</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="departmentDTO.departmentName"
        class="w-50 m-2"
        size="small"
        placeholder="请输入学院名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
  <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>
  </el-row>

  <!-- 表格 -->
  <el-table :data="departments" stripe border height="500">
    <el-table-column prop="id" sortable=true label="ID" width="65"/>
    <el-table-column prop="departmentName" sortable=true label="学院名称"  width="85"/>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updatedepartment(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deletedepartment(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="departmentDTO.capacity"
    :current-page.sync="departmentDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '学院',
      // 表格映射对象
      departments: [],
      // 修改的对象
      udepartment: {},
      // 表格查询对象
      departmentDTO: {
          departmentName: ''
      },
      // 添加学院弹出控制
      dialogFormVisible: false,
      // 修改学院弹出控制
      updateDialogFormVisible: false,
      // 添加学院实体
      department: {},
      // 学院列表总数
      count: 0,
      // 每个学院的学生列表
      students: [],
      // 每个学院的小组名称
      groups: []
    }
  },
  // 页面初始化函数 
  created () {
    this.$http({
        url: "/department",
        params: {
            departmentName: this.departmentDTO.departmentName
        }
    }).then(response => {
       this.departments = response.data
    })
  },
  methods: {
    // 改变当前页
    changeCurrent() {
      this.findAll()
    },
    // 添加学院
     adddepartment() {
       this.dialogFormVisible=true;
     },
     updatedepartment(data) {
       this.udepartment=data
       this.updateDialogFormVisible=true
     },
     deletedepartment(id) {
        this.$http.delete('/department/' + id).then(response => {
          this.$message({
          message: '删除学院成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAdddepartment() {
      this.$http.post("/department", this.department).then(response => {
        this.$message({
          message: '添加学院成功',
          type: 'success'
        });
        this.cleardepartment()
        this.findAll()
      })
    },
    // 取消添加学院
    cancelAdddepartment() {
      this.cleardepartment()
    },
    ensureUpdatedepartment() {
      this.$http.put('/department', this.udepartment).then(response => {
        this.$message({
          message: '修改学院成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedepartment() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 获得该学院的小组信息
    showGroup(id) {
      this.$http.get('/group/department/' + id).then(response => {
        this.groups = response.data
      })
    },
    // 获得该学院的学生信息
    showStudent(id) {
      this.$http.get('/student/department/' + id).then(response => {
        this.students = response.data
      })
    },
    // 查询所有，并映射
    findAll() {
      this.$http({
          url: "/department",
          params: {
              departmentName: this.departmentDTO.departmentName
          }
      }).then(response => {
        console.log( response.data)
        this.departments = response.data
      })
    },
    // 清除学院，并关闭窗口
    cleardepartment() {
        this.department.departmentName=''
        this.department.password=''
        this.department.roleId=''
        this.department.departmentNo=''
        this.dialogFormVisible=false
     },
    exportStudentExcel (departmentName) {
        /* generate workbook object from table */
        var xlsxParam = { raw: true } // 导出的内容只做解析，不进行格式转换
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#exportStudent'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), departmentName + '学生名单.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    },
    exportGroupExcel (departmentName) {
        /* generate workbook object from table */
        var xlsxParam = { raw: true } // 导出的内容只做解析，不进行格式转换
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#exportGroup'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), departmentName + '小组名单.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }
  }
}
</script>
<style>

</style>